import time

from Case.python_ui.Common.basepage import Basepage
from Case.python_ui.PageLocate.admin_personal_organization_locator import PersonalOrganizationLocator
from Case.python_ui.Test_data.organization_data import Organization


class PerSonalOrganization(Basepage):

    def enter_pesonal_organization_page(self):
        doc = '进入到personal_organization页面'
        for i in range(3):
            try:
                self.wait_elevisble(PersonalOrganizationLocator.pesonal_user_tab, doc)
                self.click_element(PersonalOrganizationLocator.pesonal_user_tab, doc)
                self.wait_elevisble(PersonalOrganizationLocator.organization_tab, doc)
                self.click_element(PersonalOrganizationLocator.organization_tab, doc)
                break
            except:
                self.refresh_page()
                continue
        self.wait_elevisble(PersonalOrganizationLocator.page_total, doc)

    def query_organization(self, org_name):
        doc = '查询组织'
        self.enter_pesonal_organization_page()
        for i in range(3):
            self.wait_elevisble(PersonalOrganizationLocator.organization_query_input, doc)
            self.keyboard_event(PersonalOrganizationLocator.organization_query_input, doc)
            self.send_keys(PersonalOrganizationLocator.organization_query_input, org_name, doc)
            self.click_element(PersonalOrganizationLocator.organization_query_button, doc)
            time.sleep(5)
            eles = self.get_elements(PersonalOrganizationLocator.organization_name_list_eles, doc)
            return eles

    def query_immediate_member(self, org_name, account_email):
        doc = '查询组织下面的成员'
        self.query_organization(org_name)
        self.wait_elevisble(PersonalOrganizationLocator.immediate_member_search_input, doc)
        self.send_keys(PersonalOrganizationLocator.immediate_member_search_input, account_email, doc)
        self.wait_elevisble(PersonalOrganizationLocator.page_total, doc)
        self.click_element(PersonalOrganizationLocator.immediate_member_query_button, doc)
        self.wait_elevisble(PersonalOrganizationLocator.immediate_member_query_result, doc)
        eles = self.get_elements(PersonalOrganizationLocator.immediate_member_query_result, doc)
        return eles

    def query_immediate_inexistence_member(self, org_name, account_email):
        doc = '查询组织下面存在的成员'
        self.query_organization(org_name)
        self.wait_elevisble(PersonalOrganizationLocator.immediate_member_search_input, doc)
        self.send_keys(PersonalOrganizationLocator.immediate_member_search_input, account_email, doc)
        self.wait_elevisble(PersonalOrganizationLocator.page_total, doc)
        self.click_element(PersonalOrganizationLocator.immediate_member_query_button, doc)
        self.wait_elevisble(PersonalOrganizationLocator.loading_wait, doc)
        self.wait_eleinvisble(PersonalOrganizationLocator.loading_wait, doc)
        time.sleep(2)
        eles = self.get_elements(PersonalOrganizationLocator.immediate_member_query_result, doc)
        return eles

    def filtering_function_retrieval(self, org_name, account_email):
        doc = '筛选状态查询'
        self.query_immediate_member(org_name, account_email)
        self.wait_eleinvisble(PersonalOrganizationLocator.loading_wait, doc)
        self.move_mouse_to_click_element(PersonalOrganizationLocator.filter, doc)
        time.sleep(5)
        self.move_mouse_to_click_element(PersonalOrganizationLocator.filter_normal, doc)
        time.sleep(2)
        self.move_mouse_to_click_element(PersonalOrganizationLocator.filter_ok, doc)
        res = self.get_element(PersonalOrganizationLocator.filter_status, doc).text
        return res

    def reset_org_user_password(self, org_name):
        doc = '重置组织下成员的密码'
        self.enter_pesonal_organization_page()
        self.query_organization(org_name)
        self.wait_elevisble(PersonalOrganizationLocator.page_total, doc)
        time.sleep(2)
        self.wait_elevisble(PersonalOrganizationLocator.reset_pwd, doc)
        self.move_mouse_to_click_element(PersonalOrganizationLocator.reset_pwd, doc)
        time.sleep(3)
        self.wait_elevisble(PersonalOrganizationLocator.delete_org_ack, doc)
        time.sleep(2)
        self.click_element(PersonalOrganizationLocator.delete_org_ack, doc)
        time.sleep(2)
        self.click_element(PersonalOrganizationLocator.operation_confirm, doc)
        self.wait_elevisble(PersonalOrganizationLocator.success_prompt, doc)

    def list_columns(self):
        doc = "组织成员列表字段校验"

        self.enter_pesonal_organization_page()

        list_columns = self.get_elements(PersonalOrganizationLocator.list_columns, doc)

        assert list_columns[0].text == "Account Email"
        assert self.get_element(PersonalOrganizationLocator.list_column_status, doc).text == "Status"
        assert list_columns[2].text == "User Name"
        assert list_columns[3].text == "Phone Number"
        assert list_columns[4].text.startswith("Creation Time")
        assert list_columns[5].text == "Actions"

        self.wait_elevisble(PersonalOrganizationLocator.status_checkbox, doc)
        self.click_by_js(PersonalOrganizationLocator.status_checkbox, doc)
        self.wait_elevisble(PersonalOrganizationLocator.status_checkbox_options, doc)
        status_options = self.get_elements(PersonalOrganizationLocator.status_checkbox_options, doc)
        assert status_options[0].text == "Normal"
        assert status_options[1].text == "Locked"
        assert status_options[2].text == "Inactive"

        assert self.get_element(PersonalOrganizationLocator.reset_pwd_button, doc).text == "Reset Password"

        self.move_mouse_to_element(PersonalOrganizationLocator.user_options_ellipsis)
        time.sleep(2)
        assert self.get_element(PersonalOrganizationLocator.lock_button, doc).text == "Lock"
        assert self.get_element(PersonalOrganizationLocator.unlock_button, doc).text == "Unlock"

    def reset_member_pwd(self):
        doc = "重置组织成员密码"
        time.sleep(5)
        self.click_element(PersonalOrganizationLocator.reset_pwd_button, doc)

        self.wait_elevisble(PersonalOrganizationLocator.accept_ack, doc)
        prompt_lines = self.get_elements(PersonalOrganizationLocator.reset_prompt_lines, doc)
        assert prompt_lines[0].text == "User must agree with you to help reset password."
        assert prompt_lines[1].text == "New password will be sent to user via selected channel(s)."

        self.click_element(PersonalOrganizationLocator.accept_ack, doc)
        time.sleep(2)
        self.click_element(PersonalOrganizationLocator.confirm_button, doc)
        self.wait_elevisble(PersonalOrganizationLocator.success_pup, doc)
        assert self.get_element(PersonalOrganizationLocator.success_pup, doc).text == "Reset password successfully."

    def lock_member(self):
        doc = "锁定组织成员"
        time.sleep(5)
        self.wait_eleinvisble(PersonalOrganizationLocator.loading_wait, doc)
        time.sleep(2)
        self.wait_elevisble(PersonalOrganizationLocator.user_options_ellipsis,doc)
        self.move_mouse_to_element(PersonalOrganizationLocator.user_options_ellipsis)
        time.sleep(2)
        self.save_screen(doc+'-点击lock之前')
        self.wait_elevisble(PersonalOrganizationLocator.lock_button_area, doc)
        self.move_mouse_to_click_element(PersonalOrganizationLocator.lock_button_area, doc)

        self.wait_elevisble(PersonalOrganizationLocator.accept_ack, doc)
        prompt_lines = self.get_elements(PersonalOrganizationLocator.lock_prompt_lines, doc)
        assert prompt_lines[0].text == "The user was confirmed to be unsecure on system."
        assert prompt_lines[1].text == "After locking account, user can not access system from next login try."

        self.click_element(PersonalOrganizationLocator.accept_ack, doc)
        time.sleep(2)
        self.click_element(PersonalOrganizationLocator.confirm_button, doc)
        self.wait_elevisble(PersonalOrganizationLocator.success_pup, doc)
        assert self.get_element(PersonalOrganizationLocator.success_pup, doc).text == "Locked successfully."

        self.refresh_page()

    def unlock_member(self):

        doc = "解锁组织成员"

        time.sleep(5)
        self.wait_eleinvisble(PersonalOrganizationLocator.loading_wait, doc)
        self.move_mouse_to_element(PersonalOrganizationLocator.user_options_ellipsis)
        time.sleep(2)
        self.click_element(PersonalOrganizationLocator.unlock_button_area, doc)

        self.wait_elevisble(PersonalOrganizationLocator.accept_ack, doc)
        prompt_lines = self.get_elements(PersonalOrganizationLocator.lock_prompt_lines, doc)
        assert prompt_lines[0].text == "The user was confirmed to be secure on system."
        assert prompt_lines[1].text == "After unlocking account, user can access system from next login try."

        self.click_element(PersonalOrganizationLocator.accept_ack, doc)
        time.sleep(2)
        self.click_element(PersonalOrganizationLocator.confirm_button, doc)
        self.wait_elevisble(PersonalOrganizationLocator.success_pup, doc)
        assert self.get_element(PersonalOrganizationLocator.success_pup, doc).text == "Unlocked successfully."

